<?php
/**
 * Created by PhpStorm
 * User: whh
 * Date: 2022/7/28
 * Time: 16:02
 */

namespace app\common\model;


use app\backstage\model\ManagerUser;
use think\Model;

class QuickUser extends Model
{
    use Common;

    protected $table = 'quick_user';


    /*
     * 返回分表
     */
    public static function fbTable($feq)
    {
        return "quick_user_" . $feq;
    }

    public function ua()
    {
        return $this->hasOne(QuickUserUa::class, 'uid', 'id')->field("uid,ua");
    }


    /*
     * 关联快应用
     */
    public function quickApp()
    {
        return $this->hasOne(QuickApp::class, 'id', 'quick_id')->field("id,name");
    }

    /*
     * 关联代理
     */
    public function agent()
    {
        return $this->hasOne(ManagerUser::class, 'id', 'agent_id')->field("id,account,parent_id");
    }

    /*
     * 关联投放号
     */
    public function channel()
    {
        return $this->hasOne(ManagerUser::class, 'id', 'channel_id')->field("id,account");
    }


    /*
     * 关联来源代理
     */
    public function sourceAgent()
    {
        return $this->hasOne(ManagerUser::class, 'id', 'source_agent_id')->field("id,account,parent_id");
    }

    /*
     * 关联来源投放号
     */
    public function sourceChannel()
    {
        return $this->hasOne(ManagerUser::class, 'id', 'source_channel_id')->field("id,account");
    }

    public function getVipTextAttr($value, $data)
    {
        $time = time();
        if ($data['vip_exp_time'] >= $time) {
            return 'vip会员';
        }
        return '普通会员';
    }

    /*
     * 格式化金额分
     */
    public function getPayMoneyTextAttr($value, $data)
    {
        return moneyFormat($data['pay_money']);
    }

    /*
     * 关联书点
     */
    public function bookPoints()
    {
        return $this->hasMany(UserBookPoints::class, 'uid');
    }

    /*
     * 关联审核人员
     */
    public function auditor()
    {
        return $this->hasOne(Auditor::class, 'userId', 'device_id')->where("status",1)->field("userId,api_version,status");
    }

    /*
     * 关联黑名单
     */
    public function black()
    {
        return $this->hasOne(UserBlack::class, 'userId', 'device_id');
    }

    /*
     * 关联白名单
     */
    public function white()
    {
        return $this->hasOne(UserWhite::class, 'uid', 'id');
    }
}